/**
 * 我的地址 - 新增地址
 */
import 'package:cbec_app/components/base/base_interactive.dart';
import 'package:cbec_app/components/base/base_page.dart';
import 'package:cbec_app/components/common/city_picker.dart';
import 'package:cbec_app/components/common/common_style.dart';
import 'package:cbec_app/components/base/base_nav_bar.dart';
import 'package:cbec_app/pages/my/controller/address_ctr.dart';
import 'package:cbec_app/utils/GlobalData.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class AddAddress extends StatefulWidget {
  AddAddress({super.key});

  @override
  State<AddAddress> createState() => _AddAddressState();
}

class _AddAddressState extends State<AddAddress> {
  final AddressController addressCtr = Get.find<AddressController>();

  // int? _value = 0;

  bool is_default = false;

  // final List<Map<String, dynamic>> _tagList = [
  //   {"tag": "家", "index": 0},
  //   {"tag": "公司", "index": 1},
  //   {"tag": "学校", "index": 2},
  //   {"tag": "学校", "index": 3},
  // ];

  @override
  Widget build(BuildContext context) {
    return BasePage(
      title: "我的地址",
      iconType: IconType.back,
      children: [
        Container(
            width: double.infinity,
            margin: EdgeInsets.all(10.w),
            child: Column(
              children: [
                //收货人的填写等区域
                SizedBox(
                  child: Card(
                    surfaceTintColor: Colors.white,
                    shadowColor: Colors.white,
                    child: Container(
                      padding: EdgeInsets.all(8.w),
                      child: Column(
                        children: [
                          //收货人
                          Container(
                            decoration: bottom_gary_border,
                            child: Row(
                              children: [
                                Text(
                                  "收货人",
                                  style: blackFont(14),
                                ),
                                SizedBox(width: 35.w),
                                Container(
                                  width: 200.w,
                                  padding: EdgeInsets.only(top: 10.w, bottom: 10.w),
                                  child: TextFormField(
                                    controller: addressCtr.nameTxtCtr,
                                    decoration: descript("请输入收货人姓名"),
                                  ),
                                )
                              ],
                            ),
                          ),
                          Container(
                            decoration: bottom_gary_border,
                            child: Row(
                              children: [
                                Text(
                                  "手机号码",
                                  style: blackFont(14),
                                ),
                                SizedBox(width: 20.w),
                                Container(
                                  width: 200.w,
                                  padding: EdgeInsets.only(top: 10.w, bottom: 10.w),
                                  child: TextFormField(
                                    controller: addressCtr.phoneTxtCtr,
                                    maxLength: 11,
                                    keyboardType: TextInputType.phone,
                                    decoration: descript("请填写收货人的手机号码"),
                                  ),
                                )
                              ],
                            ),
                          ),
                          Container(
                            decoration: bottom_gary_border,
                            child: Row(
                              children: [
                                Text(
                                  "所在地区",
                                  style: blackFont(14),
                                ),
                                SizedBox(width: 20.w),
                                Container(
                                    width: 200.w,
                                    padding: EdgeInsets.only(top: 10.w, bottom: 10.w),
                                    child: BaseInteractive(
                                      onClick: () {
                                        showModalBottomSheet(
                                          context: context,
                                          isScrollControlled: true,
                                          builder: (ctx) {
                                            return CityPickerView(
                                              initProvince: addressCtr.provinceTxt.value,
                                              initCity: addressCtr.cityTxt.value,
                                              initArea: addressCtr.areaTxt.value,
                                              params: GlobalData.provinceCityAreaList,
                                              onResult: (res) {
                                                addressCtr.choiseArea(res);
                                              },
                                            );
                                          },
                                        );
                                        // Tools.showLoading(),
                                        // await addressCtr.getProvinceCtiyArea().then((value) => {
                                        //   Tools.hideLoading(),
                                        //   showModalBottomSheet(
                                        //   context: context,
                                        //   isScrollControlled: true,
                                        //   builder: (ctx) {
                                        //     return CityPickerView(
                                        //       initProvince: addressCtr.provinceTxt.value,
                                        //       initCity: addressCtr.cityTxt.value,
                                        //       initArea: addressCtr.areaTxt.value,
                                        //       params: value,
                                        //       onResult: (res) {
                                        //         addressCtr.choiseArea(res);
                                        //       },
                                        //     );
                                        //   },
                                        // )
                                        // }),
                                      },
                                      childWidget: Obx(() => Text(addressCtr.areaText.value, style: setTextStyle(14, FontWeight.w400, addressCtr.areaColor.value))),
                                    ))
                              ],
                            ),
                          ),
                          SizedBox(
                            child: Row(
                              children: [
                                Text(
                                  "详细地址",
                                  style: blackFont(14),
                                ),
                                SizedBox(width: 20.w),
                                Container(
                                  padding: EdgeInsets.only(top: 10.w, bottom: 10.w),
                                  width: 200.w,
                                  child: TextFormField(
                                    controller: addressCtr.addressTxtCtr,
                                    decoration: descript("街道、楼牌号等"),
                                  ),
                                )
                              ],
                            ),
                          ),

                          Container(
                            decoration: border_radis(const Color.fromRGBO(248, 248, 248, 1)),
                            margin: EdgeInsets.only(top: 20.w),
                            padding: EdgeInsets.all(10.w),
                            // width: 200,
                            child: TextFormField(
                              controller: addressCtr.smartParseCtr,
                              decoration: descript("输入文试试粘贴收件人姓名、手机号、收货地址，可快速识别您的收货信息"),
                              maxLines: 3,
                            ),
                          ),
                          Container(
                            margin: EdgeInsets.only(top: 15.w, bottom: 10.w),
                            child: const Row(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [Text("地址粘贴板"), Icon(Icons.expand_less)],
                            ),
                          )
                        ],
                      ),
                    ),
                  ),
                ),
                //标签选择及设为默认地址等
                Container(
                  margin: EdgeInsets.only(top: 20.w),
                  child: Card(
                    surfaceTintColor: Colors.white,
                    shadowColor: Colors.white,
                    child: Container(
                      padding: EdgeInsets.all(10.w),
                      child: _tagBox(),
                    ),
                  ),
                ),
              ],
            )),
        Positioned(
            key: GlobalKey(debugLabel: 'Positioned'),
            bottom: MediaQuery.of(context).padding.bottom,
            child: Container(
              color: Colors.white,
              padding: EdgeInsets.fromLTRB(18.w, 10.w, 15.w, 10.w),
              child: solidButton("保存", () {
                addressCtr.submit();
              }),
            ))
      ],
    );
  }

  Widget _tagBox() => Container(
        child: Column(
          children: [
            //下边
            Container(
              margin: EdgeInsets.only(top: 0.w, bottom: 10.w),
              padding: EdgeInsets.only(top: 10.w),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Column(mainAxisAlignment: MainAxisAlignment.spaceEvenly, crossAxisAlignment: CrossAxisAlignment.start, children: [
                    Text(
                      "设置默认地址",
                      style: blackFont(14),
                    ),
                    const Text("提醒：下单会优先使用该地址", style: TextStyle(fontSize: 12, color: Color.fromRGBO(153, 153, 153, 1)))
                  ]),
                  //设为默认地址
                  Obx(() => SizedBox(
                      child: Switch(
                          activeTrackColor: const Color.fromRGBO(248, 63, 45, 1),
                          value: addressCtr.isDefault.value,
                          onChanged: (value) {
                            addressCtr.setDefault(value);
                          })))
                ],
              ),
            )
          ],
        ),
      );

  // Widget buildChoiceClip() {
  //   return Wrap(
  //     children: _tagList
  //         .map((e) => Padding(
  //               padding: EdgeInsets.only(left: 5.w, right: 5.w),
  //               child: buildItem(e),
  //             ))
  //         .toList(),
  //   );
  // }

  ///构建每一个 ChoiceChip
  // ChoiceChip buildItem(Map<String, dynamic> map) {
  //   String tag = map["tag"];
  //   int index = map["index"];
  //   return ChoiceChip(
  //     label: Text(tag),
  //     showCheckmark: false,
  //     shape: RoundedRectangleBorder(
  //       borderRadius: BorderRadius.circular(20.0), // 设置圆角
  //       side: BorderSide(
  //           color: Color.fromRGBO(243, 245, 247, 1), width: 1.0.w), // 设置边框颜色和宽度
  //     ),
  //     padding: EdgeInsets.only(left: 8.w, right: 8.w),
  //     selected: _value == index,
  //     onSelected: (bool selected) {
  //       setState(() {
  //         _value = selected ? index : null;
  //       });
  //     },
  //     labelStyle: TextStyle(
  //       color: _value == index ? Colors.white : Color.fromRGBO(51, 51, 51, 1),
  //     ),
  //     selectedColor: const Color.fromRGBO(29, 153, 255, 1),
  //     surfaceTintColor: Colors.white,
  //   );
  // }
}
